//
//  StatusCell.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/27.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
import FFLabel

@objc
protocol StatusCellDelegate: NSObjectProtocol {
    optional func labelDidSelectedLinkText(url: NSURL)
}


let StatusCellMargin: CGFloat = 12
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    weak var statusCellDelegate: StatusCellDelegate?
    
    //微博视图模型
    var statusViewModel: StatusViewModel? {
        didSet {
            topView.statusViewModel = statusViewModel
            contentLable.text = statusViewModel?.status.text
            contentLable.attributedText = EmoticonManager.sharedManager.emotionText(contentLable.text ?? "", font: contentLable.font)
            
            
            pictureView.statusViewModel = statusViewModel
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
            }
        }
    }
    
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        statusViewModel = vm
        contentView.layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
        //设置选中样式
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 懒加载视图控件
    //顶部视图
    lazy var topView: StatusCellTopView = StatusCellTopView()
    //微博正文标签
    lazy var contentLable: FFLabel = FFLabel(title: "微博正文", fontSize: 15, screenInset: StatusCellMargin)
    //微博配图
    lazy var pictureView = StatusPictureView()
    //底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
}
// MARK: - 设置界面
extension StatusCell {
   func setupUI() {
        //添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            // ?????????
//            make.right.equalTo(contentView.snp_right).offset(0 - StatusCellMargin)
        }
      
        
//        bottomView.snp_makeConstraints { (make) -> Void in
////            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentView.snp_left)
//            make.right.equalTo(contentView.snp_right)
//            make.height.equalTo(44)
//            make.bottom.equalTo(contentView.snp_bottom)
//        }
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
    
        contentLable.labelDelegate = self
    }
}
// MARK: - FFLabelDelegate
extension StatusCell: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        if text.hasPrefix("http://") {
            guard let url = NSURL(string: text) else {
                return
            }
        statusCellDelegate?.labelDidSelectedLinkText?(url)
        }
    }
}

